<?php
/**
 * PddCopyProductsDao.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2021/1/17- 下午4:51
 */

namespace common\jobs\dao;


use common\constants\CopyConst;
use common\exception\ServicesException;
use common\exception\ValidatorException;
use common\jobs\models\PddCopyProducts;
use common\library\BaseDao;
use Yii;

class PddCopyProductsDao extends BaseDao
{
    /**
     * 写入单条日志记录
     * @param array $data
     * @return PddCopyProducts
     * @throws ValidatorException
     */
    public function insertData(array $data)
    {
        $copyProduct = new PddCopyProducts();

        $copyProduct->load($data, '');

        if (!$copyProduct->validate()) {
            throw new ValidatorException($copyProduct->errorsToString());
        }

        if ($copyProduct->save()) {
            return $copyProduct;
        } else {
            throw new ValidatorException($copyProduct->errorsToOneString());
        }
    }


    /**
     * 通过主键获取信息
     * @param int $pk
     * @return PddCopyProducts|null
     */
    public function getInfoByPk(int $pk)
    {
        return PddCopyProducts::findOne(['id' => $pk]);
    }

    /**
     * 通过goods_id,批次ID获取信息
     * @param int $batch_no
     * @param int $num_iid
     * @return PddCopyProducts|null
     */
    public function getInfoById(int $batch_no, int $num_iid)
    {

        return PddCopyProducts::findOne(['batch_no' => $batch_no, 'num_iid' => $num_iid, 'status' => 0]);
    }

    public function getInfoByIdRequest(int $batch_no, int $num_iid)
    {
        return PddCopyProducts::find()->where(['batch_no' => $batch_no, 'num_iid' => $num_iid, 'status' => 0,'request_id'=>''])->one();
    }

    /**
     * 通过RequestId,批次ID获取信息
     * @param int $batch_no
     * @param int $num_iid
     * @return PddCopyProducts|null
     */
    public function getInfoByRequestId(string $request_id, int $num_iid)
    {

        return PddCopyProducts::findOne(['request_id' => $request_id, 'num_iid' => $num_iid]);
    }


    /**
     * 通过唯一主键+店铺ID 获取数据
     * @param $copyId
     * @param int $mall_id
     * @return PddCopyProducts|null
     */
    public function getInfoByCopyIdAndMallId($copyId, int $mall_id)
    {
        return PddCopyProducts::findOne(['status' => CopyConst::COPY_YES, 'mall_id' => $mall_id, 'copy_id' => $copyId]);
    }


    /**
     * 通过货源平台唯一主键+店铺id 获取数据
     * @param $num_iid
     * @param int $mall_id
     * @return PddCopyProducts|null
     */
    public function getInfoByNumIIdAndMallId($num_iid, int $mall_id)
    {
        return PddCopyProducts::findOne([
            'status' => CopyConst::COPY_YES,
            'mall_id' => $mall_id,
            'num_iid' => $num_iid,
            'to_type' => CopyConst::COPY_TO_SHOP
        ]);
    }


    /**
     * 通过货源平台唯一主键+uid+类型 获取数据
     * @param $num_iid
     * @param int $uid
     * @return PddCopyProducts|null
     */
    public function getInfoByStatusAndUidAndNumIId($num_iid, int $uid)
    {
        return PddCopyProducts::findOne([
            'status' => CopyConst::COPY_YES,
            'uid' => $uid,
            'num_iid' => $num_iid,
            'to_type' => CopyConst::COPY_TO_SHOP
        ]);
    }


    /**
     * 批量写入
     * @param array $data
     * @return int
     * @throws ServicesException
     * @throws \yii\db\Exception
     */
    public function batchInsert(array $data)
    {
        if (!count($data)) throw new ServicesException('请求参数错误');
        $fields = array_keys($data[0]);
        return Yii::$app->db->createCommand()
            ->batchInsert(PddCopyProducts::tableName(), $fields, $data)->execute();

    }

}